<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <tree-tools :tree-node="company" :is-root="true" @addDepts="addDepts"/>
        <!-- 放置一个el-tree -->
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <!-- 引入的组件 -->
          <tree-tools slot-scope="{ data }" :tree-node="data" @delDepts="getDepartments" @addDepts="addDepts" @editDepts="editDepts"/>
        </el-tree>
      </el-card>
    </div>
    <!-- 放置新增弹层组件  -->
    <add-dept ref="addDept" :show-dialog.sync='showDialog' :tree-node="node" @addDepts="getDepartments"/>
  </div>
</template>

<script>
import TreeTools from './components/tree-tools'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from "@/utils"
import AddDept from './components/add-dept' // 引入新增部门组件
export default {
  components: {
    TreeTools,
    AddDept
  },
  data () {
    return {
      company: { },
      departs: [],
      defaultProps: {
        label: 'name'
      },
      showDialog: false, // 显示窗体
      node: null // 记录当前点击的node节点
    }
  },
  created () {
    this.getDepartments()  //调用methods里的方法
  },
  methods: {
    async getDepartments(){
      const result = await getDepartments()   
      this.company = { name: result.companyName, manager: '负责人',id:''}
      this.departs = tranListToTreeData(result.depts,'')
      console.log(result);
    },
    // 添加子部门的触发函数
    addDepts(node){
      this.showDialog = true
      this.node = node
    },
    // 点击编辑部门触发的函数
    editDepts(node){
      this.showDialog = true
      this.node = node
      // 利用给组件设置ref属性，获取组件的实例，使用组件里的方法获取数据，并且传入id
      this.$refs.addDept.getDepartDetail(node.id)
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
